<template>
  <div>
    <!-- 用户头像上传 -->
    <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false"
      :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisibleImgEdit">
      <ImgEdit @resImg="resImg" :fileEdit="fileEdit"></ImgEdit>
    </el-dialog>



    
     <h1></h1>
     

    <!-- 照片墙 -->
    <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card"
      :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

  </div>
</template>

<script>
import ImgEdit from './ImgEdit.vue'
export default {
  name: 'HelloWorld',
  components:{ImgEdit},
  data() {
    return {
      // 用户头像上传
      imageUrl: '',
      dialogVisibleImgEdit:false,
      fileEdit : {},

      // 照片墙
      dialogImageUrl: '',
      dialogVisible: false
    };
  },
  methods: {
    /**
     * 用户头像上传
     * @param {*} res 
     * @param {*} file 
     */
    handleAvatarSuccess(res, file) {
      console.log('res',res);
      console.log('file',file)
      
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(1212,this.imageUrl)
    },
    beforeAvatarUpload(file) {
      console.log(file)
      
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      this.dialogVisibleImgEdit = true//开启图片编辑组件
      this.fileEdit=file//将预编辑图片传给编辑组件编辑

      return isJPG && isLt2M;
    },
    //子组件返回的数据
    resImg(data){
      console.log(data)
    },


    /**
     * 照片墙
     * @param {*} file 
     * @param {*} fileList 
     */
    handleRemove(file, fileList) {
      //file是删除的文件对象，fileList是删除后的文件数组
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }

}
</script>


<style scoped>
/* 用户头像上传 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
